package br.edu.ufcg.p1.barzin;

import static br.edu.ufcg.p1.barzin.util.PrefManConstants.BAR_NAME;
import static br.edu.ufcg.p1.barzin.util.PrefManConstants.TABLET_NAME;

import java.text.DecimalFormat;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import br.edu.ufcg.p1.barzin.event.Assinante;
import br.edu.ufcg.p1.barzin.event.EventBus;
import br.edu.ufcg.p1.barzin.event.EventoBase;
import br.edu.ufcg.p1.barzin.event.TiposDeEventos;
import br.edu.ufcg.p1.barzin.event.TotalMsgsRecuperadoEvento;

@SuppressWarnings("rawtypes")
public class BarraTopoFragment extends Fragment implements
		Assinante<EventoBase> {

	private PedidosFragment pedidosFragment;
	private MensagemFragment mensagemFragment;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View barraTopo = inflater.inflate(R.layout.barra_topo_fragment,
				container, false);

		String name = App.environment().getSharedPreferences()
				.getString(BAR_NAME, "BarName");
		name += " ("
				+ App.environment().getSharedPreferences()
						.getString(TABLET_NAME, "TabletName") + ")";

		((TextView) barraTopo.findViewById(R.id.dadosBar)).setText(name);

		((ImageView) barraTopo.findViewById(R.id.icone_pedidos))
				.setClickable(true);
		((ImageView) barraTopo.findViewById(R.id.icone_pedidos))
				.setOnClickListener(new OnClickListener() {

//					@Override
					public void onClick(View v) {
						FragmentManager fragmentManager = getFragmentManager();
						Fragment frag = fragmentManager
								.findFragmentById(R.id.telaPrincipalCardapioFragment);
						FragmentTransaction fragTran = fragmentManager
								.beginTransaction();
						fragTran.hide(frag);

						Fragment frag2 = fragmentManager
								.findFragmentById(R.id.telaPrincipalCardapioFragment);
						fragTran.hide(frag2);
					
						if (mensagemFragment != null
								&& !mensagemFragment.isHidden()) {
							fragTran.hide(mensagemFragment);
						}

						if (pedidosFragment == null) {
							pedidosFragment = new PedidosFragment();
							fragTran.add(R.id.telaPrincipalLinearlayout,
									pedidosFragment);
						} else {
							pedidosFragment.atualizaPessoas();
							pedidosFragment.desmarcaTodos();
							fragTran.show(pedidosFragment);
						}

						fragTran.commit();
					}
				});

		((ImageView) barraTopo.findViewById(R.id.cardapioIcone))
				.setClickable(true);
		((ImageView) barraTopo.findViewById(R.id.cardapioIcone))
				.setOnClickListener(new OnClickListener() {

//					@Override
					public void onClick(View v) {
						FragmentManager fragmentManager = getFragmentManager();
						FragmentTransaction fragTran = fragmentManager
								.beginTransaction();

						if (pedidosFragment != null
								&& !pedidosFragment.isHidden()) {
							fragTran.hide(pedidosFragment);
						}

						if (mensagemFragment != null
								&& !mensagemFragment.isHidden()) {
							fragTran.hide(mensagemFragment);
						}

						Fragment frag = fragmentManager
								.findFragmentById(R.id.telaPrincipalCardapioFragment);
						fragTran.show(frag);

						fragTran.commit();
					}
				});

		((ImageView) barraTopo.findViewById(R.id.icone_msg)).setClickable(true);
		((ImageView) barraTopo.findViewById(R.id.icone_msg))
				.setOnClickListener(new OnClickListener() {

//					@Override
					public void onClick(View v) {
						FragmentManager fragmentManager = getFragmentManager();
						Fragment frag = fragmentManager
								.findFragmentById(R.id.telaPrincipalCardapioFragment);
						FragmentTransaction fragTran = fragmentManager
								.beginTransaction();
						fragTran.hide(frag);

						Fragment frag2 = fragmentManager
								.findFragmentById(R.id.telaPrincipalCardapioFragment);
						fragTran.hide(frag2);

						if (pedidosFragment != null
								&& !pedidosFragment.isHidden()) {
							fragTran.hide(pedidosFragment);
						}

						if (mensagemFragment == null) {
							mensagemFragment = new MensagemFragment();
							fragTran.add(R.id.telaPrincipalLinearlayout,
									mensagemFragment);
						} else {
							mensagemFragment.populaDados();
							fragTran.show(mensagemFragment);
						}

						fragTran.commit();
					}
				});

		double total = App.environment().getDao().totalConta();

		DecimalFormat df = new DecimalFormat("0.00");
		String valor = df.format(total).replace(".", ",");

		((TextView) barraTopo.findViewById(R.id.totalDaConta)).setText("R$ "
				+ valor);

		return barraTopo;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		EventBus.getInstance().registraAssinante(
				TiposDeEventos.PEDIDO_ALTERADO, this);

		EventBus.getInstance().registraAssinante(TiposDeEventos.CONTA_FECHADA,
				this);

		EventBus.getInstance().registraAssinante(
				TiposDeEventos.TOTAL_MSG_RECUPERADO, this);
	}

//	@Override
	public void trataEvento(EventoBase evento) {
		if (evento instanceof TotalMsgsRecuperadoEvento) {
			TotalMsgsRecuperadoEvento event = (TotalMsgsRecuperadoEvento) evento;
			View v = getView();
			if (v != null) {
				TextView tv = ((TextView) v.findViewById(R.id.iconeTotalMsgs));
				if (tv != null)
					tv.setText(" " + event.getDado().getQuantidade().toString() + " ");
				if (event.getDado().getQuantidade() > 0) {
					tv.setBackgroundColor(0xFFFF0000);
				} else {
					tv.setBackgroundColor(0xFF000000);
				}
				if (mensagemFragment != null
						&& !mensagemFragment.isHidden()) {
					mensagemFragment.populaDados();
				}
			}
		} else {
			double total = App.environment().getDao().totalConta();
			DecimalFormat df = new DecimalFormat("0.00");
			String valor = df.format(total).replace(".", ",");
			View v = getView();
			if (v != null) {
				TextView tv = ((TextView) v.findViewById(R.id.totalDaConta));
				if (tv != null)
					tv.setText("R$ " + valor);
			}
		}
	}

}